import { Navigate } from 'react-router-dom'
import { lazy, Suspense } from 'react'

var NotFound = lazy(() => import('../../views/NotFound'))
var HospitalInformation = lazy(() => import('../../views/Index/HospitalInformation'))


var Doctormanagement = lazy(() => import('../../views/Index/Hospital Information/Doctormanagement'))
var Physicalexaminationpackage = lazy(() => import('../../views/Index/Hospital Information/Physicalexaminationpackage'))
var EncyclopediaofHealth = lazy(() => import('../../views/Index/Hospital Information/EncyclopediaofHealth'))
var Hospitalnavigation = lazy(() => import('../../views/Index/Hospital Information/Hospitalnavigation'))
var Hospitalintroduction = lazy(() => import('../../views/Index/Hospital Information/Hospitalintroduction'))
var Appointmentnotice = lazy(() => import('../../views/Index/Hospital Information/Appointmentnotice'))
var AddNewDoctor = lazy(() => import('../../views/Index/Hospital Information/AddNewDoctor'))

var Item = lazy(() => import('../../views/Index/Hospital Information/Children/Item'))
var Package = lazy(() => import('../../views/Index/Hospital Information/Children/Package'))
var Type = lazy(() => import('../../views/Index/Hospital Information/Children/Type'))

var NotFound = lazy(() => import('../../views/NotFound'))

function AuthComponent({ children, permission }) {
    var permissions = localStorage.getItem('userinfo') ?
        JSON.parse(localStorage.getItem('userinfo')).role.permission : [];

    //判断当前用户是否有权限访问该路由对应的组件
    var res = permission && permissions.includes(permission)

    return (
        <Suspense fallback={<div>loading...</div>}>
            {
                res ?
                    children :
                    <NotFound />
            }
        </Suspense>
    )
}

export default [//医院信息模块的二级路由
    {
        path: '/index/hospitalinformation',
        element: <AuthComponent permission={14}><HospitalInformation /></AuthComponent>,
        children: [
            {
                path: '/index/hospitalinformation/doctormanagement',
                element: <AuthComponent permission={141}><Doctormanagement /></AuthComponent>,
            },
            {
                path: '/index/hospitalinformation/physicalexaminationpackage',
                element: <AuthComponent permission={143}><Physicalexaminationpackage /></AuthComponent>,
                children: [
                    {
                        path: '/index/hospitalinformation/physicalexaminationpackage/item',
                        element: <AuthComponent permission={143}><Item /></AuthComponent>,
                    },
                    {
                        path: '/index/hospitalinformation/physicalexaminationpackage/package',
                        element: <AuthComponent permission={143}><Package /></AuthComponent>,
                    },
                    {
                        path: '/index/hospitalinformation/physicalexaminationpackage/type',
                        element: <AuthComponent permission={143}><Type /></AuthComponent>,
                    },
                    {
                        path: '/index/hospitalinformation/physicalexaminationpackage',
                        element: <Navigate to='/index/hospitalinformation/physicalexaminationpackage/package' />
                    },
                    {
                        path: '*',
                        element: <NotFound />
                    }
                ]
            },
            {
                path: '/index/hospitalinformation/encyclopediaofHealth',
                element: <AuthComponent permission={144}><EncyclopediaofHealth /></AuthComponent>,
            },
            {
                path: '/index/hospitalinformation/hospitalnavigation',
                element: <AuthComponent permission={145}><Hospitalnavigation /></AuthComponent>,
            },
            {
                path: '/index/hospitalinformation/hospitalintroduction',
                element: <AuthComponent permission={146}><Hospitalintroduction /></AuthComponent>,
            },
            {
                path: '/index/hospitalinformation/appointmentnotice',
                element: <AuthComponent permission={147}><Appointmentnotice /></AuthComponent>,
            },
            {
                path: '/index/hospitalinformation/addnewdoctor',
                element: <AuthComponent permission={14}><AddNewDoctor /></AuthComponent>,
            },
            // 二级路由重定向
            {
                path: '/index/hospitalinformation',
                element: <Navigate to='/index/hospitalinformation/physicalexaminationpackage' />
            },
            {
                path: '*',
                element: <NotFound />
            }
        ]
    },
]